<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" draggable :show-fullscreen="true" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="120px" label-position="left">
            <!-- <el-form-item label="角色" prop="set_id">
                <el-input-number v-model="form.set_id"></el-input-number>
        	</el-form-item> -->
            <el-form-item label="角色ID" prop="">
                <el-select  v-model="form.set_id" clearable placeholder="角色">
                    <el-option   v-for="(item,index) in set_id_option" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="运营中心ID" prop="run_id">
                <el-input-number v-model="form.run_id"></el-input-number>
        	</el-form-item> -->

            <el-form-item  label="运营中心ID" prop="">
                <el-select  v-model="form.run_id" clearable placeholder="运营中心">
                    <el-option   v-for="(item,index) in run_id_option" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>


            <el-form-item label="销售直推%" prop="direct1">
                <el-input-number v-model="form.direct1"></el-input-number>
        	</el-form-item>
            <el-form-item label="销售间接%" prop="indirect1">
                <el-input-number v-model="form.indirect1"></el-input-number>
        	</el-form-item>
            <el-form-item label="参与直推%" prop="direct2">
                <el-input-number v-model="form.direct2"></el-input-number>
        	</el-form-item>
            <el-form-item label="参与间接%" prop="indirect2">
                <el-input-number v-model="form.indirect2"></el-input-number>
        	</el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button v-if="mode!='read'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
    
    export default {
        emits: ['success', 'closed'],
        
        data() {
            return {
                mode: "create",
                titleMap: {
                    read: '查看',
                    create: '新增',
                    update: '编辑',
                },
                visible: false,
                isSaveing: false,
                form: {
                    id: 0,
					set_id: '',
					run_id: '',
					direct1: '',
					indirect1: '',
					direct2: '',
					indirect2: '',
                },
                rules: {
                },
                set_id_option:[],
                run_id_option:[],
            }
        },
        mounted() {
            this.getSet();//表格加载时会执行该方法
            this.getRun();//表格加载时会执行该方法
        },
        methods: {
            async getSet(){//需要加入的方法
                var res = await this.$API.set.read.get();
                this.set_id_option = res.data.list;


                // var res = await this.$API.set.read.get();
                // var list = res.data.list;
                // var set_list_option = [];
                // for (let index = 0; index < list.length; index++) {
                //     set_list_option[list[index].id] = list[index].name;                    
                // }
                // console.log(set_list_option);
                // this.set_id_option = set_list_option;
            },

            async getRun(){//需要加入的方法
              
                var res = await this.$API.run.read.get();
                this.run_id_option = res.data.list;
                // var res = await this.$API.run.read.get();
                // var list = res.data.list;
                // var run_list_option = [];
                // for (let index = 0; index < list.length; index++) {
                //     run_list_option[list[index].id] = list[index].name;                    
                // }
                // console.log(run_list_option);
                // this.run_id_option = run_list_option;
            },
            open(mode='create'){
                this.mode = mode;
                this.visible = true;
                return this
            },
            submit(){
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res = await this.$API.run_commission_set[this.mode].post(this.form);
                        this.isSaveing = false;
                        if(res.code == 200){
                            this.$emit('success', this.form, this.mode)
                            this.visible = false;
                            this.$message.success(res.message)
                        }else{
                            this.$alert(res.message, "提示", {type: 'error'})
                        }
                    }else{
                        return false;
                    }
                })
            },
            setData(data){
                this.$TABLE.assign(this,data);
            }
        }
    }
</script>